package com.prime.cn.controller;

import com.prime.cn.pojo.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class LoginController {
    @RequestMapping("/dologin")
    public String validateLogin(String username,String password){
        User u=new User(username,password);
        if("success".equals(shiroLogin(u))){
             return "redirect:/main";
        }else{
            System.out.printf("00000");
            return "redirect:/fail";
        }

    }
    public String shiroLogin(User u){
        UsernamePasswordToken upt=new UsernamePasswordToken(u.getUsername(),u.getPwd());
//        upt.setRememberMe(true);
        try{
            SecurityUtils.getSubject().login(upt);
        }catch(UnknownAccountException ex){
            return "用户不存在";
        }catch(IncorrectCredentialsException ex){
            return "密码错误";
        }catch(Exception ex){
           return "内部错误，重新登录！";
        }
        return "success";
    }
}
